package com.agent;

import java.lang.instrument.ClassDefinition;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;

/**
 * @author dingrz
 * @date 2022-01-22
 */
public class HelloAgent {

    public static void agentmain(String agentArgs, Instrumentation inst) {
        System.out.println("this is agentmain,入参："+agentArgs);
        inst.addTransformer(new MonitorDemoTransformer(), true);
        try {
            /**
             * 因为目标类已经被jvm加载过，需要对目标类进行重转换才会触发transform拦截
             */
            for (Class loadClasses : inst.getAllLoadedClasses()) {//获取JVM已经加载的所有类
                if (loadClasses.getName().contains("com.example.springbootdemo.controller")) {
                    inst.retransformClasses(loadClasses);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
